
import { DefaultLayout, GraphNode } from "@/js/components/GraphNode"
import { GraphView } from "@/js/components/GraphView"
import { Select } from "@/js/components/Input"
import { InputLinkNodeAny, InputLinkNodeBoolean, InputLinkNodeFlow, InputLinkNodeString } from "@/js/components/InputLinkNode"

class SelectInput extends Select {

}

export class SelectAttribute extends GraphNode{
    layout?: DefaultLayout
    // target?: 
    load(){
        super.load()

        const layout = new this.DefaultLayout(this, this.graphView as GraphView, false, false)
        this.layout = layout
        this.appendChild(layout.container)

        const attrName = new InputLinkNodeString(this, this.graphView as GraphView, {
            name: "attrName",
            desc: '属性',
            model: "entry",
            placeholder: '属性名称',
            input: SelectInput
        })

        layout.main?.left.appendChild(new InputLinkNodeAny(this, this.graphView as GraphView, {
            name: "target",
            desc: '目标(默认this)',
            model: "entry",
            showInput: false
        }))
        .appendChild(attrName)
        
        layout.main?.right.appendChild(new InputLinkNodeAny(this, this.graphView as GraphView, {
            name: "print",
            desc: '引用',
            model: 'output',
            showInput: false,
            linkMaxCount: 10000
        }))

        this.enters.target.event.on("link", ({target})=>{
            (attrName.input as any).setOptions( Object.getOwnPropertyNames(target.graphNode.getLinkValue() || {}) )
        })

        setTimeout(() => {
            if(this.enters.target.linkNodeLines[0] && this.enters.target.linkNodeLines[0].endPointTarget){
                const opt = (this.enters.target.linkNodeLines[0] as any).endPointTarget.graphNode as any;
                (attrName.input as any).setOptions( Object.getOwnPropertyNames( opt.getLinkValue() || {}) )
            }
        }, 10)
    }
}

SelectAttribute.parser =  ({node,next})=>{
    const target = next(node.enters.target, "this")
    const attrName = next(node.enters.attrName) || `"${node.userData.enters.attrName}"`

    return `${target}[${attrName}]`
}